<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Removing content tests</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-git.css" type="text/css" />
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="qunit/connector.js"></script>
<script type="text/javascript" src="qunit/runner.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<script type="text/javascript" src="js/tiny_mce_loader.js"></script>
<script><!--
var editor;

QUnit.config.autostart = false;
QUnit.config.reorder = false;

module("Remove Content", {
	autostart: false
});

asyncTest('Backspace with indented text', function() {
	var c, originalContent = '<p>Line1</p>\n<p style="margin-left: 30px;">Line2</p>';

	expect(2);

	editor.setContent(originalContent);
	editor.focus();
	setSelection('p:nth-child(1)', 4, 'p:nth-child(2)', 4);
	robot.type('\b', false, function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), "<p>Line2</p>", 'Delete content');
			editor.execCommand('undo', false, null);
			equals(editor.getContent(), originalContent, 'Undo deletion');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

// oncut is not supported by FireFox 2.0 so this is impossible to fix there.
if (!tinymce.isGecko || !/Firefox\/[12].[0-9]/.test(navigator.userAgent)) {
	asyncTest('Cut with indented text', function() {
		var c, originalContent = '<p>Line1</p>\n<p style="margin-left: 30px;">Line2</p>';
	
		expect(3);
	
		editor.setContent(originalContent);
		editor.focus();
		setSelection('p:nth-child(1)', 4, 'p:nth-child(2)', 4);
		robot.cut(function() {
			// The fix for this bug happens in a timeout after cut so we need to make sure that happens first by adding an additional delay.
			setTimeout(function() {
				equals(editor.getContent(), "<p>Line2</p>", 'Cut text');
				editor.execCommand('undo', false, null);
				equals(editor.getContent(), originalContent, 'Undo cut');
				editor.execCommand('SelectAll');
				robot.paste(function() {
					ok(/Line/.test(editor.getContent()), 'Content should have been on clipboard. Got: ' + editor.getContent());
					QUnit.start();
				}, editor.selection.getNode());
			}, 100);
		}, editor.selection.getNode());
	});
} else {
	test('Skipped on FireFox 2.x: Cut with indented text', function() {});
}

asyncTest('Forward delete with indented text', function() {
	var c, originalContent = '<p>Line1</p>\n<p style="margin-left: 30px;">Line2</p>';

	expect(2);

	editor.setContent(originalContent);
	editor.focus();
	setSelection('p:nth-child(1)', 4, 'p:nth-child(2)', 4);
	robot.forwardDelete(function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), "<p>Line2</p>", 'Delete content');
			editor.execCommand('undo', false, null);
			equals(editor.getContent(), originalContent, 'Undo deletion');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

asyncTest('Forward delete with first paragraph indented', function() {
	var c, originalContent = '<p style="margin-left: 30px;">Line1</p>\n<p>Line2</p>';

	expect(1);

	editor.setContent(originalContent);
	editor.focus();
	setSelection('p:nth-child(1)', 4, 'p:nth-child(2)', 4);
	robot.forwardDelete(function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), '<p style="margin-left: 30px;">Line2</p>', 'Delete content');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

asyncTest('Backward delete from paragraph to heading', function () {
	var c, originalContent = '<h1>Heading</h1><p>text</p>';

	expect(1);

	editor.setContent(originalContent);
	editor.focus();
	setSelection('p', 0);
	robot.type('\b', false, function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), '<h1>Headingtext</h1>', 'Delete content');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

asyncTest('Forward delete from heading to paragraph', function () {
	var c, originalContent = '<h1>Heading</h1><p>text</p>';

	expect(1);

	editor.setContent(originalContent);
	editor.focus();
	setSelection('h1', 7);
	robot.forwardDelete(function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), '<h1>Headingtext</h1>', 'Delete content');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

asyncTest('Forward delete from heading to paragraph with span', function () {
	var c, originalContent = '<h1>Heading</h1><p><span style="color: #ff0000;">text</span></p>';

	expect(1);

	editor.setContent(originalContent);
	editor.focus();
	setSelection('h1', 7);
	robot.forwardDelete(function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), '<h1>Heading<span style="color: #ff0000;">text</span></h1>', 'Delete content');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

asyncTest('Backward delete from paragraph with span to heading', function () {
	var c, originalContent = '<h1>Heading</h1><p><span style="color: #ff0000;">text</span></p>';

	expect(1);

	editor.setContent(originalContent);
	editor.focus();
	setSelection('p', 0);
	robot.type('\b', false, function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), '<h1>Heading<span style="color: #ff0000;">text</span></h1>', 'Delete content');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

asyncTest('Backward delete all contents', function () {
	var c, originalContent = '<h1>Heading</h1><p><span style="color: #ff0000;">text</span></p>';

	expect(1);

	editor.setContent(originalContent);
	editor.focus();
	editor.execCommand('SelectAll');
	robot.type('\b', false, function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), '', 'Empty contents');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});

asyncTest('Forward delete all contents', function () {
	var c, originalContent = '<h1>Heading</h1><p><span style="color: #ff0000;">text</span></p>';

	expect(1);

	editor.setContent(originalContent);
	editor.focus();
	editor.execCommand('SelectAll');
	robot.forwardDelete(function() {
		// The content is fixed up after a small timeout to let the browser default behaviour kick in.
		// As such we need to defer our checks as well.
		setTimeout(function() {
			equals(editor.getContent(), '', 'Empty contents');
			QUnit.start();
		}, 100);
	}, editor.selection.getNode());
});
var initTinyFunction = function(){
	tinyMCE.init({
		mode : "exact",
		elements : "elm1",
		theme : "advanced",
		add_unload_trigger : false,
		theme_advanced_styles : 'test1=test1;test2=test2',
		valid_styles : {
			'*' : 'text-align,padding-left,color,font-size,font-family,background-color,font-weight,font-style,text-decoration,float,margin,margin-top,margin-right,margin-bottom,margin-left,display'
		},
		init_instance_callback : function(ed) {
			editor = ed;

			ed.onNodeChange.addToTop(function() {
				return false;
			});
		}
	});
};
--></script>
</head>
<body>
	<h1 id="qunit-header">Removing content tests</h1>
	<h2 id="qunit-banner"></h2>
	<div id="qunit-testrunner-toolbar"></div>
	<h2 id="qunit-userAgent"></h2>
	<ol id="qunit-tests"></ol>
	<div id="content">
		<textarea id="elm1" name="elm1"></textarea>
	</div>
	<script type="text/javascript" src="jsrobot/robot.js"></script>
	<script>
		initWhenTinyAndRobotAreReady(initTinyFunction);
	</script>
</body>
</html>
